package com.guangzhi.weijianzhi.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.entity.BeansCity;
import com.guangzhi.weijianzhi.entity.BeansDistrict;
import com.guangzhi.weijianzhi.entity.BeansProvince;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.Toast;

public class ProvinceCity{

	 

	public static Map<String, List<ProvinceAndCity>> initData(Resources re) {
		// TODO Auto-generated method stub
		
		Map<String, List<ProvinceAndCity>> maps = null;
		List<String> shengs = new ArrayList<String>();
		List<ProvinceAndCity> lists =null;
		ProvinceAndCity city = null;
		
		//公司地址区域
				InputStream inputStream = re.openRawResource(
						R.raw.citys_weathertwo);
				try {
					XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
					XmlPullParser parser = factory.newPullParser();
					parser.setInput(inputStream, "utf-8");
						String sheng="";
					int eventType = parser.getEventType();

					while (eventType != XmlPullParser.END_DOCUMENT) {

						
						switch (eventType) {

						case XmlPullParser.START_TAG:
							if (parser.getName().equals("citylist")) {
								maps = new HashMap<String, List<ProvinceAndCity>>();
							}else if(parser.getName().equals("p")){
								lists = new ArrayList<ProvinceCity.ProvinceAndCity>();
							}else if((parser.getName().equals("pn"))){
								sheng =parser.nextText();
							}else if(parser.getName().equals("c")){
								city = new ProvinceAndCity();
								String id = parser.getAttributeValue(0);
								city.id = id;
							}else if(parser.getName().equals("cn")){
								String name = parser.nextText();
								city.name = name;
							}

							break;

						case XmlPullParser.END_TAG:

							if (parser.getName().equals("c")) {
								lists.add(city);
								city =null;
							}else if(parser.getName().equals("p")){
								if(!"".equals(sheng)){
									maps.put(sheng, lists);
									shengs.add(sheng);
									sheng = "";
								}
								
								
							}

							break;
						}

						eventType = parser.next();
					}

				}  catch (Exception e) {
					e.printStackTrace();
				}
					return maps;
	}

	
	
	
	public static List<String>  initDataCity(Resources re) {
		// TODO Auto-generated method stub
		
		List<String> shengs = new ArrayList<String>();
		
		//公司地址区域
				InputStream inputStream = re.openRawResource(
						R.raw.citys_weathertwo);
				try {
					XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
					XmlPullParser parser = factory.newPullParser();
					parser.setInput(inputStream, "utf-8");
						String sheng="";
					int eventType = parser.getEventType();

					while (eventType != XmlPullParser.END_DOCUMENT) {

						
						switch (eventType) {

						case XmlPullParser.START_TAG:
							 if((parser.getName().equals("pn"))){
								sheng =parser.nextText();
							}

							break;

						case XmlPullParser.END_TAG:

							 if(parser.getName().equals("p")){
								if(!"".equals(sheng)){
									shengs.add(sheng);
									sheng = "";
								}
								
								

							break;
						}
						}
						eventType = parser.next();
					}

				}  catch (Exception e) {
					e.printStackTrace();
				}
					return shengs;
	}
	
	
	
	
	
	
	
	/**
	 * 解析数据
	 */
		public static ArrayList<BeansProvince> initData(Context con) {
			ArrayList<BeansProvince> privinces = null;
			// TODO Auto-generated method stub
			InputStream inputStream = con.getResources().openRawResource(
					R.raw.citys_weather);
			try {
				XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
				XmlPullParser parser = factory.newPullParser();
				parser.setInput(inputStream, "utf-8");

				int eventType = parser.getEventType();

				BeansProvince province = null;
				ArrayList<BeansCity> citys = null;
				BeansCity city = null;
				ArrayList<BeansDistrict> districts = null;
				BeansDistrict district = null;

				while (eventType != XmlPullParser.END_DOCUMENT) {

					String tag = parser.getName();
					switch (eventType) {
					case XmlPullParser.START_DOCUMENT:
						privinces = new ArrayList<BeansProvince>();
						break;

					case XmlPullParser.START_TAG:
						if (tag.equals("p")) {
							province = new BeansProvince();
							citys = new ArrayList<BeansCity>();
							// 取出属性添加了注释
							int count = parser.getAttributeCount();
							for (int i = 0; i < count; i++) {
								String name = parser.getAttributeName(i);
								String value = parser.getAttributeValue(i);
								if (name.equals("p_id")) {
									province.setId(value);
								}
							}
						}
						if (tag.equals("pn")) {
							String name = parser.nextText();
							province.setName(name);
						}

						if (tag.equals("c")) {
							city = new BeansCity();
							districts = new ArrayList<BeansDistrict>();

							int count = parser.getAttributeCount();
							for (int i = 0; i < count; i++) {
								String name = parser.getAttributeName(i);
								String value = parser.getAttributeValue(i);
								if (name.equals("c_id")) {
									city.setId(value);
								}
							}

						}

						if (tag.equals("cn")) {
							String name = parser.nextText();
							city.setName(name);
						}

						if (tag.equals("d")) {
							district = new BeansDistrict();

							int count = parser.getAttributeCount();
							for (int i = 0; i < count; i++) {
								String name = parser.getAttributeName(i);
								String value = parser.getAttributeValue(i);
								if (name.equals("d_id")) {
									district.setId(value);
								}
							}

							String name = parser.nextText();
							district.setName(name);
							districts.add(district);

						}

						break;

					case XmlPullParser.END_TAG:

						if (tag.equals("c")) {
							city.setDistricts(districts);
							citys.add(city);
						}

						if (tag.equals("p")) {
							province.setCitys(citys);
							privinces.add(province);
						}

						break;
					}

					eventType = parser.next();
				}

			} catch (XmlPullParserException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return privinces;
		}	
	
	
	
	
	  public static class ProvinceAndCity{
		
		public String id;
		public String name;
	}

}
